//
//  MineSettingView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/26.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class MineSettingView: UIView {
    
    // 常量相关
    private let titles = ["系统公告","个人资料","报销凭证","开通会员"]
    private let pics = ["debitcardapply_24x24","customer_service_24x24","cunkuan_24x24","privatefund_24x24"]
    
    // 会员标志
    private lazy var memberImgView = UIImageView(image: UIImage(named: "avatar1"))
    // 姓名
    private lazy var usernameLabel = UILabel(title: "测试", color: UIColor.black, size: 18)
    // 上次登录时间
    private lazy var preTimeLabel = UILabel(title: "测试", color: UIColor.gray, size: 13)
    
    // MARK: 回调
    public var complete:((_ item:Int) -> Void)?
    
    public var resetUserName:String?{
        didSet {
            usernameLabel.text = resetUserName ?? ""
        }
    }
    
    public var resetPreTime:String?{
        didSet {
            preTimeLabel.text = "上次登录 \(resetPreTime ?? "")"
        }
    }
    
    public var resetMemberIndex:Int?{
        didSet {
            memberImgView.image = UIImage(named: "avatar\(resetMemberIndex ?? 1)")
        }
    }
    
    override func layoutSubviews() {
        // 头部背景
        let topBackground = UIView()
        topBackground.backgroundColor = COLOR_MINE_TOP
        topBackground.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: frame.height - MARGIN50)
        addSubview(topBackground)
        // 头像
        let headerPic = UIImageView(image: UIImage(named: "main_avatar_97x97"))
        headerPic.frame = CGRect(x: MARGIN, y: MARGIN10, width: MARGIN60, height: MARGIN60)
        addSubview(headerPic)
        // 会员标志
        memberImgView.frame.origin = CGPoint(x: 58, y: 55)
        addSubview(memberImgView)
        // 姓名
        let usernameX = headerPic.frame.maxX + MARGIN10
        let usernameW = frame.width - usernameX - MARGIN
        usernameLabel.frame = CGRect(x: usernameX, y: MARGIN20, width: usernameW, height: MARGIN25)
        addSubview(usernameLabel)
        // 上次登录时间
        let preTimeW = frame.width - usernameX - MARGIN
        preTimeLabel.frame = CGRect(x: usernameX, y: usernameLabel.frame.maxY, width: preTimeW, height: MARGIN25)
        addSubview(preTimeLabel)
        // 底部椭圆背景
        let whiteBackground = UIView()
        whiteBackground.layer.cornerRadius = 8
        let backGroundY = frame.height - MARGIN80
        let backgroundW = frame.width - MARGIN * 2
        whiteBackground.layer.masksToBounds = true
        whiteBackground.backgroundColor = UIColor.white
        whiteBackground.frame = CGRect(x: MARGIN, y: backGroundY, width: backgroundW, height: MARGIN80)
        addSubview(whiteBackground)
        // 宽度
        let titleW = backgroundW / 4
        // 底下四个布局
        for i in 0..<titles.count {
            let method = MineMethodView()
            method.tag = i
            method.pic = pics[i]
            method.lable = titles[i]
            method.frame = CGRect(x: CGFloat(i) * titleW, y: 0, width: titleW, height: MARGIN80)
            whiteBackground.addSubview(method)
            // 添加点击事件
            method.addTarget(self, action: #selector(clickItem(ui:)), for: .touchUpInside)
        }
    }
    
    @objc func clickItem(ui:UIView) -> Void {
        if let callback = complete {
            callback(ui.tag)
        }
    }
}
